/**
 * 
 */
package com.monte.export.excel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.monte.export.Column;
import com.monte.export.Configure;
import com.monte.export.DataPage;
import com.monte.export.ExportDataSource;
import com.monte.export.SimpleExportor;

/**
 * Excel导出工具
 * @author fengmengyue
 *
 */
public class ExcelDataExportor<T> extends SimpleExportor<T> {

	public ExcelDataExportor(ExportDataSource<T> dataSource,
			OutputStream outputStream,Configure configure) {
		super(dataSource, outputStream,configure);
	}

	@Override
	protected void startExport() {
		DataPage<T> dataPage = new DataPage<T>();
		dataPage.setPageSize(configure.getPageSize());
		
		SXSSFWorkbook workbook = new SXSSFWorkbook();
		Sheet sheet = workbook.createSheet(configure.getSheetName());
		int sheetIndex = 0;
		int rownum = writeHead(sheet);
		int index = 1;
		List<T> datas;
		do{
			dataPage.setCurrentPage(dataPage.getNextPage());
			dataPage = getDataPage(dataPage);
			datas = dataPage.getDatas();
			if(dataPage.getTotalCount() < datas.size()){
				dataPage.setTotalCount(datas.size());
			}
			for(int i = 0; i < datas.size(); i++){
				if(rownum >= configure.getRowsOfSheet()){
					rownum = 0;
					sheetIndex ++;
					sheet = workbook.createSheet(configure.getSheetName() + "-" + sheetIndex);
				}
				writeRow(sheet,rownum,datas.get(i),index);
				rownum++;
				index++;
			}
			//当还有下一页时，继续取数
		}while(dataPage.getNextPage() > dataPage.getCurrentPage());
		try {
			workbook.write(outputStream);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	protected void writeRow(Sheet sheet,int rownum, T t, int index) {
		Row row = sheet.createRow(rownum);
		List<Column> columns = configure.getColumns();
		Cell cell = null;
		Object fieldValue = null;
		int hasIndex = 0;
		if(configure.isIndex()){
			cell = row.createCell(0);
			cell.setCellValue(index);
			cell.getCellStyle().setAlignment(CellStyle.ALIGN_RIGHT);
			hasIndex = 1;
		}
		try{
			for(int i = 0; i < columns.size(); i++){
				cell = row.createCell(i + hasIndex);
				fieldValue = BeanUtilsBean.getInstance().getPropertyUtils().getProperty(t, columns.get(i).getFieldName());
				cell.setCellValue(fieldValue == null?"":fieldValue.toString());
			}
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	protected Integer writeHead(Sheet sheet){
		int rowNum = 0;
		Row row = sheet.createRow(rowNum);
		List<Column> columns = configure.getColumns();
		Cell cell = null;
		int hasIndex = 0;
		if(configure.isIndex()){
			cell = row.createCell(0);
			cell.setCellValue("序号");
			cell.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);
			hasIndex = 1;
		}
		for(int i = 0; i < columns.size(); i++){
			cell = row.createCell(i + hasIndex);
			cell.setCellValue(columns.get(i).getHeadName());
			cell.getCellStyle().setAlignment(CellStyle.ALIGN_CENTER);
		}
		return ++rowNum;
	}
}
